///模型响应
mixin ModelRes {
  //响应结果代码
  int get retCode;

  ///响应结果消息
  String get retMessage;

  ///是否成功
  bool get isSuccessed => retCode == 0;
}

/// 响应模型基础类
abstract class BaseModelRes with ModelRes {
  @override
  final int retCode;
  @override
  final String retMessage;

  const BaseModelRes({required this.retCode, required this.retMessage});
}

///请求结果模型
class ResultModelRes extends BaseModelRes {
  static const _codeKey = "code", _messageKey = "message", _dataKey = "data";
  final dynamic data;

  ///构造函数
  const ResultModelRes({
    required super.retCode,
    required super.retMessage,
    this.data,
  });

  ///获取数据Map
  Map<String, dynamic> get toMap {
    if (data != null && data is Map) {
      return data;
    }
    return {};
  }

  ///获取数据List
  List<dynamic> get toList {
    if (data != null && data is List) {
      return data;
    }
    return [];
  }

  ///读取数据处理
  factory ResultModelRes.fromMap(final Map<String, dynamic> dataMap) {
    final data = dataMap.containsKey(_dataKey) ? dataMap[_dataKey] : null;
    return ResultModelRes(
      retCode: dataMap[_codeKey] ?? 0,
      retMessage: dataMap[_messageKey] ?? '',
      data: data,
    );
  }
}
